var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
// post请求会用到
var bodyParser = require('body-parser');
require('body-parser-xml')(bodyParser);
var ejs = require('ejs');

var app = express();  
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html',ejs.__express);
app.set('view engine', 'html');
//app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false, limit: '1000mb' }));
// parse application/json
app.use(bodyParser.json()); // request entity too large
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.xml({
  limit: '1MB',   // Reject payload bigger than 1 MB 
  xmlParseOptions: {
    normalize: true,     // Trim whitespace inside text nodes 
    normalizeTags: true, // Transform tags to lowercase 
    explicitArray: false // Only put nodes in array if >1 
  }
}));


// 设置请求头
// application/json  接口返回json数据
// charset=utf-8 解决json数据中中文乱码
app.use("*", function(req, res, next) {
	res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Credentials", true);
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 4.2.1');
	res.writeHead(200, { "Content-Type": "application/json;charset=utf-8" });
	next();
});



// git
var index = require('./routes/index');
app.use('/', index);

// ali IOT
var aliIOT = require('./routes/iot');
app.use('/', aliIOT);


// 404 错误
var errorData_404 = {
	status: '404', 
	msg: 'Not Found!',
};
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  res.end(JSON.stringify(errorData_404));
});

// 500 
var errorData_500 = {
	status: '500', 
	msg: 'Not Found!',
};
app.use(function(err, req, res, next) {
  errorData_500.msg =  err.message;
  res.end(JSON.stringify(errorData_500));
});

module.exports = app;
